package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;


    // 处理分页过滤查询员工请求
    // /employee/list 目前不带参数, 响应 HTML 格式内容
    @RequiredPermission(name="员工页面", expression = "employee:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        // 获取 PageResult 相关信息, 存到 Model 中
        model.addAttribute("pageInfo", employeeService.query(qo));
        // 查询部门信息,存到 Model 中
        model.addAttribute("departments", departmentService.listAll());
        // 找视图
        return "employee/list"; // /WEB-INF/views/employee/list.jsp
    }

    // /employee/delete?id=id值
    // 处理查询所有员工删除的请求,希望浏览器重新发送一次请求再查询一次
    @RequiredPermission(name="员工删除", expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list"; // 让浏览器再发送一次请求 localhost/employee/list
    }

    // 处理去保存或修改请求 /employee/input
    @RequiredPermission(name="员工新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("employee", employeeService.get(id));
        }
        model.addAttribute("roles", roleService.listAll());
        model.addAttribute("departments", departmentService.listAll());
        System.out.println(employeeService);
        return "employee/input";
    }

    // 处理保存的请求 /employee/saveOrUpdate, POST 携带参数
    @RequiredPermission(name="员工新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() != null) {
            employeeService.update(employee, ids);
        }else {
            employeeService.save(employee, ids);
        }
        return "redirect:/employee/list";
    }

    @RequiredPermission(name="员工导出", expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response,@ModelAttribute("qo") EmployeeQueryObject qo) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");

        Workbook wb = employeeService.exportXls(qo);

        wb.write(response.getOutputStream());
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String username) {
        Map<String, Boolean> map = new HashMap<>();
        Employee employee = employeeService.selectByUsername(username);
        map.put("valid", employee == null);
        return map;
    }

    @RequiredPermission(name="员工导入", expression = "employee:importXls")
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }
}
